package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;
import com.yuanqi.dz.dc.adapters.YinShiReadFile;
import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqi.dz.dc.util.FtpUtil;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class YinshiDzServiceImpl implements DZDataInterface{
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");


	public DZInfoArray getDZData(String mercharnum, String dz_date) {
		DZInfoArray dzInfoArray = new DZInfoArray();

		try {

			String  is_private=DZConfigRead.getProperties("is_private");
			String source_type=null;
			String  address=null;
			String local_path =null;
			String fileName=null;
			String dz_date_format=null;
			String dz_date_addday=null;
			String ftp_port =null;
			String  username=null;
			String password = null;
			String  remotePath=null ;
			if(null!=is_private && "0".equals(is_private)){
				String  is_private_mercharnum=DZConfigRead.getProperties("is_private_"+mercharnum);
				if(null!=is_private_mercharnum && is_private_mercharnum.equals("1")){
					source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
					address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
					local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
					fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
					dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
					dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
					ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
					username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
					password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
					remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
				}else{
					source_type=DZConfigRead.getProperties("source_type");
					address=DZConfigRead.getProperties("ftp_ip");
					local_path=DZConfigRead.getProperties("local_path");//本地目录
					fileName=DZConfigRead.getProperties("fileName");// 文件名称
					dz_date_format=DZConfigRead.getProperties("dz_date_format");//对账文件时间格式
					dz_date_addday=DZConfigRead.getProperties("dz_date_addday");  
					ftp_port=DZConfigRead.getProperties("ftp_port");
					username=DZConfigRead.getProperties("ftp_username");
					password=DZConfigRead.getProperties("ftp_password");
					remotePath=DZConfigRead.getProperties("ftp_filepath");
				}

			}else{
				source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
				address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
				local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
				fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
				dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
				ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
				username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
				password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
				remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
			}

			Date d= com.yuanqi.dz.dc.util.DateUtil.stringToDate(dz_date, "yyyyMMdd");
			String s= com.yuanqi.dz.dc.util.DateUtil.getDateAfter(d, "1").replaceAll("-", ""); // t + 1 模式
			//String s0 = com.yuanqi.dz.dc.util.DateUtil.getDateAfter(d, "0").replaceAll("-", ""); // t + 0模式

			//String filename0 = fileName.replace("{merchant}", mercharnum).replace("{date}", s0);
			fileName = fileName.replace("{merchant}", mercharnum).replace("{date}", s);
			File file = new File(local_path, fileName);
		/*	File file0 = new File(local_path, filename0); */
			
			List<DZInfo> list = new ArrayList<DZInfo>();
		/*	List<DZInfo> list0 = new ArrayList<DZInfo>();*/
			
			int port = StringHelper.isEmpty(ftp_port) ? 21 : Integer.valueOf(ftp_port);
			
			if("2".equals(source_type)){
				boolean download = false;
				try {
					download = new FtpUtil(address,port,username,password,remotePath).downFile(file);
				} catch (Exception e) {
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp异常");
					LOG.addError("连接ftp异常"+e);
					return dzInfoArray;
				}
				if (!download) {
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp上未找到对账文件"+file.getName());
					LOG.addError("ftp上未找到对账文件"+file.getName());
					return dzInfoArray;
				}
				list= new YinShiReadFile().getListReadFileYinshi(file, mercharnum,dz_date);
			}

		/*	// t+0 模式读取昨天的对账文件，来获取数据      
			if(!file0.exists()){
				Boolean download2 = false;
				try {
					download2 = new FtpUtil(address,port,username,password,remotePath).downFile(file0);
				} catch (Exception e) {
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp异常");
					LOG.addError("第二次连接ftp异常"+e);
					//return dzInfoArray;
				}
				if (!download2) {
					dzInfoArray.setRc("-1");
					dzInfoArray.setErrormsg("ftp上未找到对账文件"+file0.getName());
					LOG.addError("ftp上未找到对账文件"+file0.getName());
					return dzInfoArray;
				}			
			}
			list0= new YinShiReadFile().getListReadFileYinshi(file0, mercharnum,dz_date);*/

			List<DZInfo> listall = new ArrayList<DZInfo>();
			
			if( null!=list && list.size() >0 ) listall.addAll(list);
		/*	if( null!=list0 && list0.size() >0 ) listall.addAll(list0);*/

			if(null == listall || listall.size() == 0){
				dzInfoArray.setRc("-1");
				dzInfoArray.setDzInfo(new DZInfo[0]);
				dzInfoArray.setErrormsg("获取对账数据为空");
				return dzInfoArray;
			}
			
			dzInfoArray.setRc("1");
			dzInfoArray.setErrormsg("获取对账数据成功！");
			dzInfoArray.setDzInfo(listall.toArray(new DZInfo[listall.size()]));

		} catch (Exception e) {
			LOG.addError("读取银石对账文件数据异常:"+e);
			dzInfoArray.setRc("-1");
			dzInfoArray.setDzInfo(new DZInfo[0]);
			dzInfoArray.setErrormsg("获取对账数据异常");
			return dzInfoArray;
		}


		return dzInfoArray;
	}




	public static void main(String[] args) {
		/*String dz_date = "20170717";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

		Date d= com.yuanqi.dz.dc.util.DateUtil.stringToDate(dz_date, "yyyyMMdd");


		//calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dz_date));
		String s= com.yuanqi.dz.dc.util.DateUtil.getDateAfter(d, "1");
		System.out.println(s);*/


		String s = "2017-09-19 08:58:01".replaceAll("-", "").substring(0, 8);
		System.out.println(s);
	}

}
